package com.rtx.wms.std.qc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.qc.bean.ApiQc;
import com.rtx.wms.std.qc.bean.ApiQcDetail;
import com.rtx.wms.std.qc.mapper.ApiQcMapper;
import com.rtx.wms.std.qc.service.ApiQcDetailService;
import com.rtx.wms.std.qc.service.ApiQcService;

import cn.hutool.core.util.IdUtil;

@Service
public class ApiQcServiceImpl extends ServiceImpl<ApiQcMapper, ApiQc> implements ApiQcService {
	
	@Autowired
	ApiQcDetailService apiQcDetailService;

	@Transactional
	@DataSourceAnnotation
	@Override
	public void insertApiQcAndDetail(String datasourcekey,ApiQc o) {
		String uid = IdUtil.getSnowflakeNextId()+"";
		o.setBatchid(uid);
		for(ApiQcDetail qd:o.getDetails()) {
			qd.setBatchid(uid);
			apiQcDetailService.save(qd);
		}
		this.save(o);
	}
	
	@DataSourceAnnotation
	@Override
	public List<ApiQc> queryApiQcAndDetailBycondition(String datasourcekey,Wrapper<ApiQc> query) {
		List<ApiQc> list = this.list(query);
		for(ApiQc qc:list) {
			LambdaQueryWrapper<ApiQcDetail> qdqw = Wrappers.lambdaQuery();
			qdqw.eq(ApiQcDetail::getBatchid, qc.getBatchid());
			List<ApiQcDetail> details = apiQcDetailService.list(qdqw);
			qc.setDetails(details);
		}
		return list;
	}
	
	@DataSourceAnnotation
	public void updStatus(String datasourcekey,Wrapper<ApiQc> upd) {
		this.update(upd);
	}
}
